package com.gdth.bank.activity;

import java.util.ArrayList;
import java.util.List;

import com.gdth.coder.CodeUtil;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.model.Item;
import com.gdth.service.PhoneCallService;
import com.gdth.service.PhoneCallServiceConnection;
import com.gdth.utils.ToolUtil;
import com.gdth.widget.dwonloadlist.DrawDownListTextView;
import com.gdth.bank.R;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends BaseActivity{

	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left) TextView txt_header_left;
    @ViewInject(id=R.id.header_btn_right) TextView txt_header_right;

    @ViewInject(id=R.id.btn_save_recorder, click="saveRecorder") Button btn_save_recorder;
	@ViewInject(id=R.id.btn_start_service, click="startService") Button btn_start_service;
	@ViewInject(id=R.id.btn_stop_service, click="stoptService") Button btn_stop_service;
	@ViewInject(id=R.id.btn_query_recorder, click="queryRecorder") Button btn_query_recorder;

	@ViewInject(id=R.id.btn_database, click="database") Button btn_database;
	@ViewInject(id=R.id.btn_recorder, click="recorder") Button btn_recorder;

	@ViewInject(id=R.id.newWork, click="newWork") Button btn_newWork;
	@ViewInject(id=R.id.btn_jiami, click="jiami") Button btn_jiami;
	@ViewInject(id=R.id.txt_rx) TextView resultTV;
	
	@ViewInject(id=R.id.txt_type) DrawDownListTextView txt_type;
	
	PhoneCallServiceConnection myServiceConnection;
	
	private List<Item> itemPhone;
	
	private List<String> keys;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initDate();
		
		initView();
	}
	
	private void initDate(){
		String[] types = getResources().getStringArray(R.array.select_phone_recorader);
		
		itemPhone = new ArrayList<Item>();
			
		for (String val : types){
			Item mItem = new Item();
			mItem.setKey(val);
			mItem.setValue(val);
			
			itemPhone.add(mItem);
		}
		
		keys = CodeUtil.newPrivateKey();
	}
	
	private void initView(){
		txt_header_title.setText(getResources().getString(R.string.param241));
		txt_header_left.setVisibility(View.GONE);
		txt_header_right.setVisibility(View.GONE);
		
		txt_type.setLabel(getResources().getString(R.string.parai242));
		txt_type.setItem(itemPhone);
		
//		myServiceConnection = new PhoneCallServiceConnection(this, "ddd", "15820273739");;\
		
		Button btn_life = (Button) findViewById(R.id.btn_jointolife);
		btn_life.setText("click");
		btn_life.setOnClickListener(joinToLifeActivity);
		
		Button btn_grid = (Button) findViewById(R.id.btn_jointogrid);
		btn_grid.setText("测试grdview");
		btn_grid.setOnClickListener(joinToGridView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return super.onOptionsItemSelected(item);
	}
	
	/***************************点击事件****************************/
	
	public void saveRecorder(View arg0){
//		ToolUtil.saveType(this, txt_type.getValue());
	}
	
	public void startService(View arg0){
		Intent intent = new Intent(this, PhoneCallService.class); 
		bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE);  
	}
	
	public void stopService(View arg0){
		unbind();
	}
	
	public void queryRecorder(View arg0){
		Intent intent = new Intent(this, MediaListActivity.class);
		this.startActivity(intent);
	}
	
	/**
	 * 网络测试
	 * @param arg0
	 */
	public void newWork(View arg0) {
		
    }
	
	public void database(View arg0){
		Intent intent = new Intent(MainActivity.this, NoteActivity.class);
		startActivity(intent);
	}
	
	public void ditu(View arg0){
		Intent intent = new Intent(MainActivity.this, BaiduMapActivity.class);
		startActivity(intent);
	}
	
	public void recorder(View arg0){
		Intent intent = new Intent(MainActivity.this, MediaRecorderActivity.class);
		startActivity(intent);
	}

	OnClickListener joinToLifeActivity = new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(MainActivity.this, LifeActivity.class);
			startActivity(intent);
		}

	};
	
	OnClickListener joinToGridView = new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
//			Intent intent = new Intent(MainActivity.this, AccessListActivity.class);
//			startActivityForResult(intent,1);
		}

	};
	
	String encodeData = null;
	String sign = null;
	
	public void jiami(View arg0){
		String val = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
				+ "<data>"
				+ "<head>"
				+ "<ver>协议版本</ver>"  
				+ "<dev_type>设备类型</dev_type>" 
				+ "<dev_sub_type>设备子类型</dev_sub_type>"
				+ "<dev_sn>设备编号</dev_sn>"
				+ "<cmd >命令字</cmd >"  
				+ "<id>消息id</id>"
				+ "</head>"
				+ "<data/>";
		String privateKey = keys.get(0);
		String publicKey = keys.get(1);
		encodeData = CodeUtil.encryptByPublic(val, publicKey);
		sign = CodeUtil.newSign(encodeData, privateKey);
	}
	
	public void jiemi(View arg0){
		String privateKey = keys.get(0);
		String publicKey = keys.get(1);
		boolean isTrue = CodeUtil.isVerify(encodeData, sign, publicKey);
		
		if(isTrue){
			String val = CodeUtil.decrypt(encodeData, privateKey);
			//System.out.println(val);
		}
		
	}
	
	/***************************点击事件结束***************************/
	
	@Override
	protected void onDestroy() {
		unbind();   
		super.onDestroy();
	}
	
	private void unbind(){
		if (myServiceConnection != null)
			this.unbindService(myServiceConnection);
	}
}
